// QPMig.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "QPMig.h"
#include "cQPMigrationSheet.h"
#include "cNotesFolderInfo.h"
#include "cUpdateNotesDocument.h"
#include "cUser.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CQPMigApp

BEGIN_MESSAGE_MAP(CQPMigApp, CWinApp)
	//{{AFX_MSG_MAP(CQPMigApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQPMigApp construction

CQPMigApp::CQPMigApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CQPMigApp object

CQPMigApp theApp;


static BOOL Is256ColorSupported()
{
	BOOL bRetval = FALSE;

	// return TRUE if screen deivce supports 256 colors or better

	HDC hdc = GetDC(NULL);
	if (hdc != NULL)
	{
		if(GetDeviceCaps(hdc, BITSPIXEL) >= 8)
			bRetval = TRUE;
		ReleaseDC(NULL, hdc);
	}

	return bRetval;
}


/////////////////////////////////////////////////////////////////////////////
// CQPMigApp initialization

BOOL CQPMigApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}
	LNSetThrowAllErrors(TRUE);   // Throw all LNSTATUS errors
	if(FAILED(CoInitialize(NULL)))
		{
			AfxMessageBox(IDP_COM_INIT_FAILED);
			return FALSE;

		}

	AfxEnableControlContainer();
	CBitmap bmpWatermark;
	CBitmap bmpHeader;
	stControl.lpMapAddress=NULL;
	stControl.hMapFile=NULL;

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

	if (Is256ColorSupported())
	{
		VERIFY(bmpWatermark.LoadBitmap(IDB_WATERMARK256));
		VERIFY(bmpHeader.LoadBitmap(IDB_BANNER256));
	}
	else
	{
		VERIFY(bmpWatermark.LoadBitmap(IDB_WATERMARK16));
		VERIFY(bmpHeader.LoadBitmap(IDB_BANNER16));
	}
	InitData();
	char szCurDir[MAX_PATH];
	char szCurDir2[MAX_PATH+10];
	strcpy(szCurDir2,"\\\\?\\");
	WCHAR wszCurDir[1024];          // Unicode user name

	GetCurrentDirectory(MAX_PATH,szCurDir);
	strcat(szCurDir2,szCurDir);
	MultiByteToWideChar( CP_ACP, 0, szCurDir2,
        strlen(szCurDir2)+1, wszCurDir,   
     sizeof(wszCurDir)/sizeof(wszCurDir[0]) );



	cQPMigrationSheet dlg("Quickplace Data Extractor", NULL, 0, bmpWatermark, NULL, bmpHeader);
	dlg.m_psh.hInstance = ::GetModuleHandle(NULL);
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	
	SetCurrentDirectoryW(wszCurDir);

	if (nResponse == ID_WIZFINISH )
	{
		StoreData();
	}
	dlg.CleanUp();
	CoUninitialize(); 
	return FALSE;
}	

void CQPMigApp::InitData()
{
	
	stControl.m_bDoLog=GetPrivateProfileInt("QPMigration","Debug",0,"QPMIGWIZ.ini");
	stControl.fIsNotesInitialized=FALSE;
	CStdioFile cfConf;
	CString cData;
	stControl.m_strNotesIni;//="h:\\lotus\\notes\\notes.ini";
	stControl.m_strDirectoryPath;//="I:\\Projects\\QuickPlace Migration v2\\Export";
	//stControl.m_strNotesPwd="QPPassword";
	stControl.m_strQuickPlacePath="quickplace";
	if(cfConf.Open("QP.cfg",CFile::modeRead))
		{
			
			cfConf.ReadString(stControl.m_strLDAPPath);
			//cfConf.ReadString(stControl.m_strSTSAdminPassword);
			cfConf.ReadString(stControl.m_strDirectoryPath);
			cfConf.ReadString(stControl.m_strNotesIni);
			cfConf.ReadString(cData);
			cfConf.ReadString(stControl.m_strQuickPlacePath);
			cfConf.ReadString(cData);
			if(!cData.IsEmpty())
				stControl.m_bDirectorySecurity=atoi(cData);
			cfConf.ReadString(theApp.stControl.m_strOwnerAccount);;
			cfConf.ReadString(theApp.stControl.m_strOwnerEMail);
			cfConf.ReadString(cData);
			if(!cData.IsEmpty())
				stControl.m_bShowInSiteDirectory=atoi(cData);
			cfConf.ReadString(stControl.m_strUserDataFileName);
			cfConf.ReadString(cData);
			if(!cData.IsEmpty())
				stControl.m_bSubRoomListing=atoi(cData);
			cfConf.ReadString(cData);
			if(!cData.IsEmpty())
				stControl.m_bUseDescriptionForBody=atoi(cData);
			cfConf.ReadString(cData);
			if(!cData.IsEmpty())
				stControl.m_bUseFolderForMoreThanTwoAttachments=atoi(cData);
			cfConf.ReadString(stControl.m_strCustomProperties);
	
			
			cfConf.Close();
		}
			
 
	
}

void CQPMigApp::StoreData()
{
	CString cData;
	CStdioFile cfConf;	
	if(cfConf.Open("QP.cfg",CFile::modeWrite|CFile::modeCreate))
		{
			cfConf.WriteString(stControl.m_strLDAPPath);
			cfConf.WriteString("\n");
			cfConf.WriteString(stControl.m_strDirectoryPath);
			cfConf.WriteString("\n");
			cfConf.WriteString(stControl.m_strNotesIni);
			cfConf.WriteString("\n");
			cfConf.WriteString(stControl.m_strNotesPwd);
			cfConf.WriteString("\n");
			cfConf.WriteString(stControl.m_strQuickPlacePath);
			cfConf.WriteString("\n");
			cData.Format("%d", stControl.m_bDirectorySecurity);
			cfConf.WriteString(cData);
			cfConf.WriteString("\n");
			cfConf.WriteString(theApp.stControl.m_strOwnerAccount);;
			cfConf.WriteString("\n");
			cfConf.WriteString(theApp.stControl.m_strOwnerEMail);
			cfConf.WriteString("\n");
			cData.Format("%d\n", stControl.m_bShowInSiteDirectory);
			cfConf.WriteString(cData);
			cfConf.WriteString(stControl.m_strUserDataFileName);
			cfConf.WriteString("\n");
			cData.Format("%d\n", stControl.m_bSubRoomListing);
			cfConf.WriteString(cData);
			cData.Format("%d\n", stControl.m_bUseDescriptionForBody);
			cfConf.WriteString(cData);
			cData.Format("%d\n", stControl.m_bUseFolderForMoreThanTwoAttachments);
			cfConf.WriteString(cData);
			cfConf.WriteString(stControl.m_strCustomProperties);
			cfConf.WriteString("\n");
			cfConf.Flush();
			
			cfConf.Close();
		}

}

void CQPMigApp::DoLog(LPCSTR pstrLog)
{
	if(stControl.m_bDoLog)
		{
			FILE *fpLogFile=NULL;
			if((fpLogFile=fopen("Log.txt","a"))!=NULL)
				{
					fprintf(fpLogFile,"%s\r\n",pstrLog);
					fclose(fpLogFile);
				}
		}
}
